﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ClassLibraryDatabase;

namespace ClassLibraryBaseClass
{
	class Favorites
	{
		private int m_nUserID;
		private int m_nMovieID;
		public Favorites(int UserID, int MovieID)
		{
			m_nUserID = UserID;
			m_nMovieID = MovieID;
		}
		public int UserID { get { return m_nUserID; } }
		public int MovieID { get { return m_nMovieID; } }
		/// <summary>
		/// 添加自己到数据库
		/// </summary>
		/// <returns>添加成功为True，失败为False</returns>
		public bool AddFavoriteMovie()
		{
			return AddFavoriteMovie(m_nUserID , m_nMovieID);
			//string[] name = { "UserID", "MovieID" };
			//string[] value = { m_nUserID.ToString(), m_nMovieID.ToString() };
			//return DataBaseClass.insertData(name, value, "AddFavoriteMovie", GetConStr());
		}
		/// <summary>
		/// 获得数据库连接字符串
		/// </summary>
		/// <returns>数据库连接字符串</returns>
		public static string GetConStr()
		{
			return DataBaseClass.GetConnectionString();
		}
		/// <summary>
		/// 添加一条记录到数据库
		/// </summary>
		/// <param name="userID">添加记录的用户编号</param>
		/// <param name="movieID">添加记录的电影编号</param>
		/// <returns>添加成功为True，失败为False</returns>
		public static bool AddFavoriteMovie(int userID, int movieID)
		{
			try
			{
				string[] name = { "UserID", "MovieID" };
				string[] value = { userID.ToString(), movieID.ToString() };

				return DataBaseClass.insertData(name, value, "Favourites", GetConStr());
			}
			catch (SqlException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (ArgumentException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (InvalidOperationException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (Exception e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine("未知错误:" + e.Message);
				return false;
			}
		}
		/// <summary>
		/// 从数据库中删除自己的纪录
		/// </summary>
		/// <returns>删除成功为True，失败为False</returns>
		public bool DeleteFavoriteMovie()
		{
			return DeleteFavoriteMovie(m_nUserID , m_nMovieID);
		}
		/// <summary>
		/// 从数据库中删除一条记录
		/// </summary>
		/// <param name="userID">删除的纪录用户编号</param>
		/// <param name="movieID">删除的纪录的电影编号</param>
		/// <returns></returns>
		public static bool DeleteFavoriteMovie(int userID , int movieID)
		{
			string sql = "DELETE Favourites WHERE UserID=" +userID.ToString() + " AND MovieID="+movieID.ToString();
			try
			{
				return DataBaseClass.deleteData(sql, GetConStr());
			}
			catch (SqlException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (ArgumentException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (InvalidOperationException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (Exception e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine("未知错误:" + e.Message);
				return false;
			}
		}
		/// <summary>
		/// 判断此条记录是不是在数据库中
		/// </summary>
		/// <returns>在数据库中返回True，不在数据库中返回False</returns>
		public bool IsInDatabase()
		{
			return IsInDatabase(m_nUserID , m_nMovieID);
		}
		/// <summary>
		/// 判断一条记录是不是在数据库中
		/// </summary>
		/// <param name="userID">记录的用户编号</param>
		/// <param name="movieID">纪录的电影编号</param>
		/// <returns>在数据库中返回True，不在数据库中返回False</returns>
		public static bool IsInDatabase(int userID, int movieID)
		{
			try
			{
				DataTable table = DataBaseClass.selectData("SELECT * FROM Favourites WHERE UserID=" + userID.ToString() + " AND MovieID=" + movieID.ToString(), GetConStr());
				return table.Rows.Count != 0;
			}
			catch (SqlException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (ArgumentException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (InvalidOperationException e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine(e.Message);
				return false;
			}
			catch (Exception e)
			{
				System.Console.Write("Favorites类：");
				System.Console.WriteLine("未知错误:" + e.Message);
				return false;
			}
		}
		/// <summary>
		/// 返回此条记录中存储的电影编号指向的信息
		/// </summary>
		/// <returns>电影信息</returns>
		public Movie GetMovieInThis()
		{
			return Movie.GetMovieByID(m_nMovieID.ToString());
		}
		/// <summary>
		/// 由DataTable获得存储相应数据的List<Movie>对象
		/// </summary>
		/// <param name="table">DataTable对象</param>
		/// <returns>List<Movie>对象</returns>
		public static List<Movie> TranslateDataTableToList(DataTable table)
		{
			List<Movie> list = new List<Movie>();
			for (int i = 0; i < table.Rows.Count; ++i)
			{
				list.Add(GetMovie(table.Rows[i]));
			}
			return list;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="row"></param>
		/// <returns></returns>
		public static Movie GetMovie(DataRow row)
		{
			if (row == null)
				return null;
			return new Movie((int)row["ID"] , row["Name"] as string , row["Director"] as string , row["Actors"] as string , (DateTime)row["Release"] , (int)row["Length"] , row["Language"] as string , row["Type"] as string , row["NTSC"] as string , (decimal)row["Grade"] , row["Image"] as string , row["Intro"] as string );
		}
		public static List<Favorites> GetListOfFavoritesByDatatable(DataTable table)
		{
			List<Favorites> list = new List<Favorites>();
			foreach (DataRow row in table.Rows)
			{
				list.Add(GetFavorites(row));
			}
			return list;
		}
		static Favorites GetFavorites(DataRow row)
		{
			if (row == null)
				return  null;
			return new Favorites((int)row["UserID"], (int)row["MovieID"]);
		}
		//在之后获得用户的函数需要自己添加
	}
}
